#include "calculator.h"
#include "ps2.h"
#include "vga.h"
#include "config.h"


volatile int operand1     = 0;
volatile int operand2     = 0;
/*start state */
unsigned int clear   = 0;
/* 404 means no valid button pushed*/
unsigned int BTN     = 404;
unsigned int clear_counter=0;


void startCalculator(void);
void add_new1(unsigned int btn);
#ifdef DECIMAL_POINT
void add_new11(unsigned int x,unsigned int btn);
#endif
void got_number(unsigned int btn,unsigned int task);
void got_number2(unsigned int operator); /* after decimal point*/
unsigned int got_operand(unsigned int btn);
void write_result(unsigned int operator);



void startCalculator(void)
{
	/* operator */
	unsigned int task=0;
	/* current number of characters */
	unsigned int counter = 0;
	/*buffer to read operands*/
	unsigned int temp[MAX_NUM_OF_CHARACTERS];
	/* for cycle variable */
	unsigned int i;
	unsigned int counter2=0;

	clearDisplay();
	
	while(TRUE)
	{
		/* watchdog timer set up should be here if necessary */
		/*wait_ms(200);*/
		/* first execution of the loop (initialzation)*/
		if(clear)
		{
			task=0;
			counter=0;
			BTN=404;
			counter2=0;
			operand2=0;
			operand1=0;
			clear=0;
			clearDisplay();

			for(i=0; i < MAX_NUM_OF_CHARACTERS; i++)
			{
				temp[i] = 0;
			}
			i=0;
		}
		BTN = getCaracter();
		/* if BTN has a valid value */
		if(BTN!=404)			  
		{

				if((BTN < 10) &&                       /* got a number 0...9 */
					(counter < MAX_NUM_OF_CHARACTERS)) /* the character buffer is not full */
				{
					got_number(BTN, task);
					temp[counter]=BTN;
					counter++;
				}
				else if((task) &&  /* an operator already given and*/
					(BTN == 15))  /* the enter was typed display the result*/
				{
					write_result(task);
					task=0;
					counter=0;
					operand1=0;


				}
				else if((BTN > 10) && (BTN < 15) && (task == 0)) /* got an operand */
				{
					operand2 = operand1;
					operand1 = 0;
					task = got_operand(BTN);
					counter=0;
				} else if(BTN == 16)
				{
					clearDisplay();
				}
		}
	}
}



void got_number(unsigned int btn, unsigned int task)
{
	operand1=btn+(operand1*10);
	/* write operand to the display*/
	if(task)
	{
		printNumber((int)operand1, 14, 11);
	}
	else
	{
		printNumber((int)operand1, 14, 10);
	}
}




unsigned int got_operand(unsigned int btn)
{
	 unsigned int task;
	 switch(btn)
	 {
		case 11:
			task = 1;
			printOperand(12);
			break;
		case 12:
			task = 2;
			printOperand(14);
			break;
		case 13:
			task = 3;
			printOperand(11);
			break;
		case 14:
			task = 4;
			printOperand(16);
			break;
	 }	
	return task;
}

void write_result(unsigned int operator)
{

	if(operator == 1)operand2 += operand1;
	else if(operator == 2)operand2 = operand2 - operand1;
	else if(operator == 3)operand2 = operand1 * operand2;
	else if(operator == 4)
	{
		/* division by zero*/
		if((operand1 == 0) && (operator == 4))
		{
			printError();
			operand1 = 0;
			operand2 = 0;
			return;
		}
		else operand2 = operand2 / operand1;
	}



	printNumber(operand2, 14, 13);
	operand1=0;
}


